package pdf;

import com.adobe.acrobat.Viewer;

import javax.swing.*;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;

/**
 * Class to display a PDF document in a JFrame
 */
public class PDFDocument extends JPanel
{
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private Viewer viewer;

  public PDFDocument( String filename )
  {
    // Set the layout for this panel
    this.setLayout( new BorderLayout() );

    // Create the viewer
    try
    {
      viewer = new Viewer();
      FileInputStream fis = new FileInputStream( filename );
      viewer.setDocumentInputStream( fis );
      this.add( viewer, BorderLayout.CENTER );
      viewer.activate();
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
  }

  /**
 *  Displays the PDF document frame.
 */
public void begin()
  {
    JFrame frame = new JFrame( "PDF Viewer" );
    frame.setSize( 1024, 768 );
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation( d.width/2 - 512, d.height/2 - 384 );
    frame.getContentPane().add(this);
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    frame.setVisible( true );
	frame.addWindowListener(new WindowAdapter() {
		@Override
		public void windowClosed(WindowEvent arg0) {
			File f=new File("temp.pdf");
			f.delete();
		}
	});
  }
}